/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.control
{
	import ascb.util.NumberUtilities;
	
	import com.edi.runtime.component.EDIApplication;
	
	import flash.external.ExternalInterface;
	
	import mx.controls.Label;
	import mx.formatters.NumberFormatter;

	/**
	 * 计算列渲染器. 
	 * 
	 */		
	public class CaculateRender extends Label
	{
		private var _caculateScript:String;
		private var _formmater:String;
		private var _useThousandsSeparator:Boolean = false;
		private var _precision:int = 2;
		/****
		 * none     直接截取
		 * up       向上四舍五入
		 * down     向下四舍五入
		 * nearest  向最近的取数字 
		 */
		private var _rounding:String = "up";
		
		/****
		 * 参考@EDIDataGridCaculateColumn类中该属性
		 */
		private var _caculateType:String = "node";
		
		
		private var _showCharAtFirst:String = "";
		
		private var _showCharAtLast:String = "";
		
		//private var _formatter:NumberFormatter = new NumberFormatter();
		
		public function set caculateScript(value:String):void{
			if(value){
				var index:int = value.indexOf("|");
				if (index>0){
					_caculateScript = value.substr(0,index);
					_formmater = value.substr(index+1,value.length);
				}else
					this._caculateScript = value;	
			}
					
		}

		public function get caculateScript():String{
			return this._caculateScript;
		}
		
		public function get caculateType():String{
			return this._caculateType;
		}
		
		public function set caculateType(value:String):void{
			if(value)
				this._caculateType = value;
		}
		
		public function set useThousandsSeparator(value:Boolean):void{
			this._useThousandsSeparator = value;
		}
		public function get useThousandsSeparator():Boolean{
			return this._useThousandsSeparator;
		}
		
		public function set precision(value:int):void{
			this._precision = value;
		}
		public function get precision():int{
			return this._precision;
		}
		
		public function set rounding(value:String):void{
			if(value)
				this._rounding = value;	
		}
		
		public function get rounding():String{
			return this._rounding;
		}
		
		
		public function get showCharAtFirst():String{
			return this._showCharAtFirst;
		}
		
		public function set showCharAtFirst(value:String):void{
			if(value)
				this._showCharAtFirst = value;
		}
		
		public function get showCharAtLast():String{
			return this._showCharAtLast;
		}
		
		public function set showCharAtLast(value:String):void{
			if(value)
				this._showCharAtLast = value;
		}
		
		public function CaculateRender(){
			this.percentHeight = 100;
			this.percentWidth = 100;
//			this.hohorizontalScrollPolicy="off";
//            this.verticalScrollPolicy="off";
		}
		
//		public function get formatter():String{
//			return this._formatter;
//		}
//		
//		public function set formatter(value:String):void{
//			if(value)
//				this._formatter = value;
//		}
		
		/**
		 * 重载data的set方法，进行列的计算
		 * 根据caculateScript进行计算，caculateScript的格式如下：
		 * ({列名}操作符{列名})操作符{列名}
		 */
		override public function set data(value:Object):void{
			var row:XML = value as XML;
			if (row == null){
				return;
			}
			if (this._caculateScript == null){
				return;
			}
			if(this._caculateType == null){
				return;
			}
			
			var pattern:RegExp = /\[.*?\]/ig; 			//   /{.*?}/ig;
			
			var result:Object = pattern.exec(_caculateScript);
			var otherScript = _caculateScript;
			EDIApplication.outputDebug("计算脚本为："+otherScript);
			var ppp:RegExp = /,/gi;
			if(this._caculateType.toLowerCase() == "attribute"){ //计算属性
				while(result != null){
					
					var fieldName:String = result.toString();
					EDIApplication.outputDebug("result: "+result);
					EDIApplication.outputDebug("row: "+row);
					var fieldNode:XMLList = row.attribute(fieldName.substr(2,fieldName.length-3));
					EDIApplication.outputDebug("result2: "+fieldName.substr(2,fieldName.length-3));
					EDIApplication.outputDebug("fieldNode: "+fieldNode);
					if (!fieldNode){
						this.text = fieldName+"不存在";
						super.data = this.text;
						return;
					}
					var fieldValue:String="";
					
					if(null!=fieldNode && fieldNode.toString()!=""){
						EDIApplication.outputDebug("fieldNode123321: "+fieldNode);
						fieldValue = String(fieldNode).split(",").join("");
						EDIApplication.outputDebug("fieldValue: "+fieldValue);
					}
				
					fieldValue = fieldValue == "" ? "0":fieldValue;
				
					otherScript = otherScript.replace(fieldName,fieldValue);
					result = pattern.exec(_caculateScript);
				}
			}else{//计算节点 
				while(result != null){
					var fieldName:String = result.toString();
					var fieldNode:XMLList = row.child(fieldName.substr(1,fieldName.length-2));
					if (!fieldNode){
						this.text = fieldName+"不存在";
						super.data = this.text;
						return;
					}
					var fieldValue:String="";
					if(fieldNode.text()!=null && fieldNode.text()!="")
						fieldValue = String(fieldNode.text()).split(",").join("");
//						.replace(ppp,"");
				
					fieldValue = fieldValue == "" ? "0":fieldValue;
				
					otherScript = otherScript.replace(fieldName,fieldValue);
					result = pattern.exec(_caculateScript);
				}
			}
				
				
			
			
			EDIApplication.outputDebug("解析后脚本为："+otherScript);
			EDIApplication.outputDebug("otherScript: "+otherScript);
			var caculateValue:String = ExternalInterface.call("eval",otherScript);

			caculateValue = (caculateValue == null || caculateValue == "" || caculateValue.toLowerCase()=="infinity")?"0":caculateValue;
			EDIApplication.outputDebug("caculateValue: "+caculateValue);
			if (_formmater != null && _formmater != ""){
				caculateValue = NumberUtilities.round(Number(caculateValue),Number(_formmater)).toFixed(_formmater.length-1).toString();
				
			}
			text = caculateValue;
			if(text.toLowerCase()=="nan"){
				text = "0.00";
			}
			var numformatter:NumberFormatter = new NumberFormatter();
			numformatter.precision = this._precision;
			numformatter.useThousandsSeparator = this._useThousandsSeparator;
			numformatter.rounding = this._rounding;
			text = numformatter.format(Number(caculateValue));
			if(this._showCharAtFirst){
				text = this._showCharAtFirst+text;
			}
			if(this._showCharAtLast){
				text =text+ this._showCharAtLast;
			}
			EDIApplication.outputDebug("text: "+text);
			super.data = (text.toLowerCase()=="nan")?"0.00":text;
			
//			super.data = text;
		}
		
	}
}